package com.wrs.examsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wrs.examsystem.entity.Result.R;
import com.wrs.examsystem.entity.Student;
import com.wrs.examsystem.entity.Test;
import com.wrs.examsystem.entity.Topic;
import com.wrs.examsystem.entity.User;
import com.wrs.examsystem.mapper.TestMapper;
import com.wrs.examsystem.mapper.TopicMapper;
import com.wrs.examsystem.mapper.UserMapper;
import com.wrs.examsystem.service.TestService;
import com.wrs.examsystem.service.TopicService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 王荣升
 * @since 2024-05-15
 */
@Service
public class TopicServiceImpl extends ServiceImpl<TopicMapper, Topic> implements TopicService {
    @Resource
    TopicMapper topicMapper;
    @Resource
    TestMapper testMapper;

    @Override
    public R deleteTopicIdById(Integer topicId) {
        //还要删除test里面的选题数量
        QueryWrapper<Topic> topicQueryWrapper = new QueryWrapper<>();
        SQLException exception = new SQLException();
        //删除题目Topic中的
        topicQueryWrapper.eq("topic_id", topicId);
        Topic topic = topicMapper.selectOne(topicQueryWrapper);
        int deleteById = topicMapper.deleteById(topicId);
        if (deleteById == 0 && topic != null) {
            return R.error().message("删除失败");
        }

        //删除test里面的选题数量
        QueryWrapper<Test> testQueryWrapper = new QueryWrapper<>();
        testQueryWrapper.eq("test_id", topic.getTestId());
        int delete = testMapper.delete(testQueryWrapper);
        if (deleteById == 0 || delete == 0) {
            exception.printStackTrace();
        }
        return R.ok();


    }

    @Override
    public R insertTopic(Topic topic) {
         topicMapper.insert(topic);
         return R.ok().data(topic);
    }

    @Override
    public R updateTopic(Topic topic) {
        topicMapper.updateById(topic);
        return R.ok();
    }


}
